//
//  EventFilterViewController.m
//  jungoo
//
//  Created by Dang Quang Trung/Sinbad Flyce on 8/28/12.
//  Copyright (c) 2012 VMODEV. All rights reserved.
//

#import "EventFilterViewController.h"
#import "Extensions.h"
#import "JGoAPI.h"

#define JGO_CF_BLUE ([UIColor colorWithRed:1./255 green:167./255 blue:237./255 alpha:1.0])
#define JGO_CF_GRAY ([UIColor colorWithRed:102./255 green:102./255 blue:102./255 alpha:1.0])

@implementation EventFilterViewController

@synthesize delegate = _delegate;

#pragma mark - Private

- (void)initAppearance
{
    if (_lastFilter == nil) {
        _lastFilter = @"0;0;0";
    }
    
    NSArray *filters = [_lastFilter componentsSeparatedByString:@";"];
    
    whenEvent = [[NSMutableArray arrayWithObjects:@"Today",@"Tomorrow",@"This Week", nil] retain];
    whatEvent = [[NSMutableArray arrayWithObjects:@"Education",@"Career",@"Social", nil] retain];
    onlaEvent = [[NSMutableArray arrayWithObjects:@"Online",@"Real Life", nil] retain];
    dataSource = [[NSMutableDictionary dictionaryWithObjectsAndKeys:[filters objectAtIndex:0],whenEvent,
                  [filters objectAtIndex:1],whatEvent,[filters objectAtIndex:2],onlaEvent,nil] retain];
    
    [self setRightButtonImageActiveName:nil
                           inactiveName:@"done-button"
                                 target:self
                                 action:@selector(done:)];
    
    [self setLeftButtonImageActiveName:nil
                          inactiveName:@"cancel-button"
                                target:self
                                action:@selector(back:)];
    [self setTitle:@"Filter Event"];
    
    [self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_bg"]]];
}

- (UITableViewCell*)filterEventCell
{
    NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"FilterEventCell"
                                                   owner:nil options:nil];
    
    return (id)[views objectAtIndex:0];
}

- (UIView*)tableHeaderView
{
    NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"FilterEventCell"
                                                   owner:nil options:nil];
    
    return (id)[views objectAtIndex:1];
}

#pragma mark - View

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initAppearance];
}

- (void)dealloc
{
    [super dealloc];
}

#pragma mark - Action

- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)done:(id)sender
{
    NSString *lastFilter = [NSString stringWithFormat:@"%@;%@;%@",
                            [dataSource objectForKey:whenEvent],
                            [dataSource objectForKey:whatEvent],
                            [dataSource objectForKey:onlaEvent]];
    
    if ([_delegate respondsToSelector:@selector(eventFilterViewController:lastFilter:)]) {
        [_delegate eventFilterViewController:self lastFilter:lastFilter];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *hv = [self tableHeaderView];
    UILabel *titleHeader = (id)[ hv viewWithTag:2];
    
    if (section == 0) {
        titleHeader.text = @"When";
    }

    if (section == 1) {
        titleHeader.text = @"What Kind of Event";
    }

    if (section == 2) {
        titleHeader.text = @"Online/Real Life";
    }

    return hv;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 || section == 1) {
        return 3;
    }
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSString *filterText = nil;
    
    cell = [tableView dequeueReusableCellWithIdentifier:@"filterEVC"];
    
    if (cell == nil) {
        cell = [self filterEventCell];
    }
    
    if (indexPath.section == 0) {
        filterText = [whenEvent objectAtIndex:indexPath.row];
        NSInteger valueCheck = [[dataSource objectForKey:whenEvent] integerValue];
        
        if (valueCheck == indexPath.row + 1) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            [cell titleTextWithTag:1].font = [UIFont boldSystemFontOfSize:17];
            [cell titleTextWithTag:1].textColor = JGO_CF_BLUE;
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryNone;
            [cell titleTextWithTag:1].font = [UIFont systemFontOfSize:17];
            [cell titleTextWithTag:1].textColor = JGO_CF_GRAY;
        }
    }

    if (indexPath.section == 1) {
        filterText = [whatEvent objectAtIndex:indexPath.row];
        NSInteger valueCheck = [[dataSource objectForKey:whatEvent] integerValue];
        
        if (valueCheck == indexPath.row + 1) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            [cell titleTextWithTag:1].font = [UIFont boldSystemFontOfSize:17];
            [cell titleTextWithTag:1].textColor = JGO_CF_BLUE;
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryNone;
            [cell titleTextWithTag:1].font = [UIFont systemFontOfSize:17];
            [cell titleTextWithTag:1].textColor = JGO_CF_GRAY;
        }
    }

    if (indexPath.section == 2) {
        filterText = [onlaEvent objectAtIndex:indexPath.row];
         NSInteger valueCheck = [[dataSource objectForKey:onlaEvent] integerValue];
        
        if (valueCheck == indexPath.row + 1) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            [cell titleTextWithTag:1].font = [UIFont boldSystemFontOfSize:17];
            [cell titleTextWithTag:1].textColor = JGO_CF_BLUE;
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryNone;
            [cell titleTextWithTag:1].font = [UIFont systemFontOfSize:17];
            [cell titleTextWithTag:1].textColor = JGO_CF_GRAY;
        }
    }
    
    [cell titleTextWithTag:1].text = filterText;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
        
    if (indexPath.section == 0) {
        
        NSInteger valueCheck = [[dataSource objectForKey:whenEvent] integerValue];
        
        if (valueCheck == indexPath.row + 1) {
            valueCheck = 0;
        }
        else {
            valueCheck = indexPath.row + 1;
        }
        
        [dataSource setObject:[NSString stringWithFormat:@"%d",valueCheck] forKey:whenEvent];
    }
    
    if (indexPath.section == 1) {
        
        NSInteger valueCheck = [[dataSource objectForKey:whatEvent] integerValue];
        
        if (valueCheck == indexPath.row + 1) {
            valueCheck = 0;
        }
        else {
            valueCheck = indexPath.row + 1;
        }
        
        [dataSource setObject:[NSString stringWithFormat:@"%d",valueCheck] forKey:whatEvent];
    }
    
    if (indexPath.section == 2) {
        
        NSInteger valueCheck = [[dataSource objectForKey:onlaEvent] integerValue];
        
        if (valueCheck == indexPath.row + 1) {
            valueCheck = 0;
        }
        else {
            valueCheck = indexPath.row + 1;
        }
        
        [dataSource setObject:[NSString stringWithFormat:@"%d",valueCheck] forKey:onlaEvent];
    }
    
    [tableView reloadData];
}

@end
